//
//  ViewControllerWave.m
//  核心动画
//
//  Created by chichaojituan521 on 2017/6/1.
//  Copyright © 2017年 linch. All rights reserved.
//

#import "ViewControllerWave.h"
#import "CircleView.h"
#import "ShanView.h"
#import "QiuView.h"
#import "QRefreshView.h"
#import "QRefreshView2.h"
@interface ViewControllerWave ()
{
    CADisplayLink *link;
    float offset;
    float speed;
    float waveWidth;
    float zhenFu;
    float changLiang;
    CAShapeLayer *layer1;
    CAShapeLayer *layer2;
    CAShapeLayer *shapeLayer;
    CircleView *viewCircle;
    float jiaoDu;
    ShanView *viewShan;
    QiuView *viewQiu;
    
    
}
@end

@implementation ViewControllerWave

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    NSArray *arrName = @[@"波浪动画",@"波浪进度",@"环形进度",@"环形进度FZ",@"扇形进度条",@"球形进度",@"的趣的hud",@"",@"",@""];
    
    for (int i = 0;i<10 ; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        btn.backgroundColor = [UIColor orangeColor];
        float JG = (self.view.frame.size.width - 70*5)/6.0;
        float btnx = JG + (JG + 70)*(i%5);
        float btny = 70 + (JG+30)*(i/5);
        btn.frame = CGRectMake(btnx, btny, 70, 30);
        [self.view addSubview:btn];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:arrName[i] forState:UIControlStateNormal];
    }

    
    
}
-(void)btnClick:(UIButton *)sender
{
    switch (sender.tag) {
        case 0:
            [self waveShow];
            break;
        case 1:
            [self hudWaveShow];
            break;
        case 2:
            [self circleProgressHud];
            break;
        case 3:
            [self circleProgressHudFZ];
            break;
        case 4:
            [self shanXingProgress];
            break;
        case 5:
            [self qiuXingProgress];
            break;
        case 6:
            [self funyWaitProgress];
            break;
        default:
            break;
    }
}
#pragma  mark 波浪
-(void)waveShow
{
    offset = 200;
    speed = 2;
    waveWidth = self.view.frame.size.width;
    zhenFu = 10;
    changLiang = 400;
    layer1 = [[CAShapeLayer alloc]init];
    layer1.opacity = 0.5;
    
    layer2 = [[CAShapeLayer alloc]init];
    layer2.opacity = 0.5;
    [self.view.layer addSublayer:layer1];
    [self.view.layer addSublayer:layer2];

    
    link = [CADisplayLink displayLinkWithTarget:self selector:@selector(doAni)];
    [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
-(void)doAni
{
    offset += speed;
    //设置第一条波曲线的路径
    CGMutablePathRef pathRef = CGPathCreateMutable();
    //起始点
    CGFloat startY = zhenFu*sinf(offset*M_PI/waveWidth);
    CGPathMoveToPoint(pathRef, NULL, 0, startY);
    //第一个波的公式
    for (CGFloat i = 0.0; i < waveWidth; i ++) {
        CGFloat y = zhenFu*sinf(2.5*M_PI*i/waveWidth + offset*M_PI/waveWidth) + changLiang;
        CGPathAddLineToPoint(pathRef, NULL, i, y);
    }
    CGPathAddLineToPoint(pathRef, NULL, waveWidth, 440);//设置波浪底部
    CGPathAddLineToPoint(pathRef, NULL, 0, 440);
    CGPathCloseSubpath(pathRef);
    //设置第一个波layer的path
    layer1.path = pathRef;
    layer1.fillColor = [UIColor lightGrayColor].CGColor;
    CGPathRelease(pathRef);
    
    
    
    
    //设置第二条波曲线的路径
    CGMutablePathRef pathRef2 = CGPathCreateMutable();
    CGFloat startY2 = zhenFu*sinf(offset*M_PI/waveWidth + M_PI/4);
    CGPathMoveToPoint(pathRef2, NULL, 0, startY2);
    //第二个波曲线的公式
    for (CGFloat i = 0.0; i < waveWidth; i ++) {
        CGFloat y = zhenFu*sinf(2.5*M_PI*i/waveWidth + 3*offset*M_PI/waveWidth + M_PI/4) + changLiang;
        CGPathAddLineToPoint(pathRef2, NULL, i, y);
    }
    CGPathAddLineToPoint(pathRef2, NULL, waveWidth, 440);
    CGPathAddLineToPoint(pathRef2, NULL, 0, 440);
    CGPathCloseSubpath(pathRef2);
    
    layer2.path = pathRef2;
    layer2.fillColor = [UIColor lightGrayColor].CGColor;
    CGPathRelease(pathRef2);
}

#pragma mark -- 波浪hud
-(void)hudWaveShow
{
    offset = 200;
    speed = 5;
    waveWidth = 200;
    zhenFu = 5;
    changLiang = 200;
    layer1 = [[CAShapeLayer alloc]init];
    layer1.opacity = 0.5;
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 200, 200)];
    view.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:view];
    view.layer.cornerRadius = 100;
    view.clipsToBounds = YES;
    view.layer.borderWidth = 1.f;
    view.layer.borderColor = [UIColor redColor].CGColor;
    
    
    [view.layer addSublayer:layer1];

    
    link = [CADisplayLink displayLinkWithTarget:self selector:@selector(doHudAni)];
    [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(waveHeghtChange) userInfo:nil repeats:YES];
    [timer fire];
}


-(void)doHudAni
{
    offset += speed;
    //设置第一条波曲线的路径
    CGMutablePathRef pathRef = CGPathCreateMutable();
    //起始点
    CGFloat startY = zhenFu*sinf(offset*M_PI/waveWidth);
    CGPathMoveToPoint(pathRef, NULL, 0, startY);
    //第一个波的公式
    for (CGFloat i = 0.0; i < waveWidth; i ++) {
        CGFloat y = zhenFu*sinf(3*M_PI*i/waveWidth + offset*M_PI/waveWidth) + changLiang;
        CGPathAddLineToPoint(pathRef, NULL, i, y);
    }
    CGPathAddLineToPoint(pathRef, NULL, waveWidth, 200);//设置波浪底部
    CGPathAddLineToPoint(pathRef, NULL, 0, 200);
    CGPathCloseSubpath(pathRef);
    //设置第一个波layer的path
    layer1.path = pathRef;
    layer1.fillColor = [UIColor greenColor].CGColor;
    CGPathRelease(pathRef);
    
}


-(void)waveHeghtChange
{
    changLiang -= 1;
    
    if (changLiang < 0) {
        changLiang = 200;
    }
    
}

#pragma mark -- 环形动画
-(void)circleProgressHud
{
    //第一步，通过UIBezierPath设置圆形的矢量路径
    UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];
    
    //第二步，用CAShapeLayer沿着第一步的路径画一个完整的环（颜色灰色，起始点0，终结点1）
    CAShapeLayer *bgLayer = [CAShapeLayer layer];
    bgLayer.frame = CGRectMake(0, 0, 200, 200);//设置Frame
    bgLayer.position = self.view.center;//居中显示
    bgLayer.fillColor = [UIColor clearColor].CGColor;//填充颜色=透明色
    bgLayer.lineWidth = 2.f;//线条大小
    bgLayer.strokeColor = [UIColor grayColor].CGColor;//线条颜色
    bgLayer.strokeStart = 0.f;//路径开始位置
    bgLayer.strokeEnd = 1.f;//路径结束位置
    bgLayer.path = circle.CGPath;//设置bgLayer的绘制路径为circle的路径
    [self.view.layer addSublayer:bgLayer];//添加到屏幕上
    
    //第三步，用CAShapeLayer沿着第一步的路径画一个红色的环形进度条，但是起始点=终结点=0，所以开始不可见
    shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = CGRectMake(0, 0, 200, 200);
    shapeLayer.position = self.view.center;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.lineWidth = 2.f;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.strokeStart = 0;
    shapeLayer.strokeEnd = 0;
    shapeLayer.path = circle.CGPath;
    [self.view.layer addSublayer:shapeLayer];
    
    UISlider *selder = [[UISlider alloc]initWithFrame:CGRectMake(10, 120, 350, 20)];
    [self.view addSubview:selder];
    
    selder.maximumValue = 1;
    selder.minimumValue = 0;
    selder.continuous = YES;
    [selder addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];// 针对值变化添加响应方法
    
}

-(void)sliderValueChanged:(UISlider *)selder
{
    UISlider *slider = (UISlider *)selder;
    shapeLayer.strokeEnd = slider.value;
    
}

#pragma  mark -- 环形进度条封装
-(void)circleProgressHudFZ
{
    viewCircle = [[CircleView alloc]initWithFrame:CGRectMake(100, 200, 200, 200)];
    [self.view addSubview:viewCircle];
    [viewCircle setLineWidth:6.f];
    [viewCircle setLineColr:[UIColor orangeColor]];
    
    
    
    UISlider *selder = [[UISlider alloc]initWithFrame:CGRectMake(10, 120, 350, 20)];
    [self.view addSubview:selder];
    
    selder.maximumValue = 1;
    selder.minimumValue = 0;
    selder.continuous = YES;
    [selder addTarget:self action:@selector(sliderValueChangedFZ:) forControlEvents:UIControlEventValueChanged];// 针对值变化添加响应方法
    
    
}

-(void)sliderValueChangedFZ:(UISlider *)selder
{
    UISlider *slider = (UISlider *)selder;
    viewCircle.shapeLayer.strokeEnd = slider.value;
    
}

#pragma  mark -- 扇形进度条
-(void)shanXingProgress
{
    viewShan = [[ShanView alloc]initWithFrame:CGRectMake(50, 180, 300, 400)];
    viewShan.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:viewShan];
    
    UISlider *selder = [[UISlider alloc]initWithFrame:CGRectMake(10, 120, 350, 20)];
    [self.view addSubview:selder];
    
    selder.maximumValue = 1;
    selder.minimumValue = 0;
    selder.continuous = YES;
    [selder addTarget:self action:@selector(sliderValueChangedShanXing:) forControlEvents:UIControlEventValueChanged];
    
    
    
}
    


-(void)sliderValueChangedShanXing:(UISlider *)selder
{
    UISlider *slider = (UISlider *)selder;
    viewShan.progressValue = slider.value;
    
}
#pragma mark --球形进度
-(void)qiuXingProgress
{
    viewQiu = [[QiuView alloc]initWithFrame:CGRectMake(50, 180, 300, 400)];
    viewQiu.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:viewQiu];
    
    UISlider *selder = [[UISlider alloc]initWithFrame:CGRectMake(10, 120, 350, 20)];
    [self.view addSubview:selder];
    
    selder.maximumValue = 1;
    selder.minimumValue = 0;
    selder.continuous = YES;
    [selder addTarget:self action:@selector(sliderValueChangedQiu:) forControlEvents:UIControlEventValueChanged];
}

-(void)sliderValueChangedQiu:(UISlider *)selder
{
    UISlider *slider = (UISlider *)selder;
    viewQiu.progressValue = slider.value;
    
}
#pragma mark --有趣的等待hud
-(void)funyWaitProgress
{
    QRefreshView *view1 = [[QRefreshView alloc]initWithFrame:CGRectMake(150, 200, 100, 100)];
    [self.view addSubview:view1];
    
    
    QRefreshView2 *view2 = [[QRefreshView2 alloc]initWithFrame:CGRectMake(150, 350, 100, 100)];
    [self.view addSubview:view2];
    
    [view1 beginAni];
    
    [view2 beginAni];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBarHidden = YES;
}

@end
